#include "ssicloud_behaviour_summary.h"

using namespace ssicloud;
BehaviourSummary::BehaviourSummary()
    : ListObject<BehaviourLNode>(LQ_BASE_URL "/customerSingleVehicle/behaviour/summary/new"
                              , OBJECT_NAME_BehaviourSummary
                              , NetUrl::NET_HTTP_METHOD_GET)
{
    // mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("vinList", VIN_STRING);
}

bool BehaviourSummary::requestAll(const char *start, const char *end)
{
    SF_ASSERT(start);
    SF_ASSERT(end);

    strcpy(m_start, start);
    strcpy(m_end, end);

    mUrl.appendChange("start", m_start);
    mUrl.appendChange("end", m_end);

    return obtain();
}

int BehaviourSummary::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    //GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *categories = cJSON_GetObjectItem(root, "data");
    if (categories)
    {
        for (cJSON *item = categories->child; NULL != item; item = item->next )
        {
            BehaviourLNode tmp;

            JSON_VALUETRING_SWAP_BYTESTRING(item, type, tmp.type);
            JSON_VALUETRING_SWAP_BYTESTRING(item, name, tmp.name);
            JSON_VALUETRING_SWAP_BYTESTRING(item, cnt, tmp.cnt);

            // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
            mNodes.push_back(tmp);
        }
        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        }/* else
        {
            profile();
        }*/
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void BehaviourSummary::profile()
{
    ListTable<BehaviourLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------驾驶行为-总览--------");
    for (; it != mNodes.end(); ++it)
    {
        GEN_Printf(LOG_DUMP, "type: %s, %s, 次数: %s", it->type.string(), it->name.c_str(), it->cnt.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

BehaviorScore::BehaviorScore()
    : ItemObject<BehaviorScoreNode>(LQ_BASE_URL "/behavior/findTripBehaviorScore"
                           , OBJECT_NAME_BehaviorScore
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}
bool BehaviorScore::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);

    return obtain();
}

void BehaviorScore::profile()
{
    GEN_Printf(LOG_INFO, "---------驾驶行为-分数--------");
    GEN_Printf(LOG_DUMP, "次数: %g, %s, %s", mItem.score, mItem.chassisNo.c_str(), mItem.plateNo.c_str());
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int BehaviorScore::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        JSON_VALUETRING_GET_DOUBLE(records, score, mItem.score);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, chassisNo, mItem.chassisNo);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, plateNo, mItem.plateNo);
        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

BehaviourClassifySummary::BehaviourClassifySummary()
    : ItemObject<BehaviourClassifySummaryNode>(LQ_BASE_URL "/app/behaviour/classify/summary"
                              , OBJECT_NAME_BehaviourClassifySummary
                              , NetUrl::NET_HTTP_METHOD_GET)
{
    // mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("vin", VIN_STRING);
}


bool BehaviourClassifySummary::requestAll(const char *behaviorType, const char *start, const char *end)
{
    SF_ASSERT(start);
    SF_ASSERT(end);

    strcpy(m_start, start);
    strcpy(m_end, end);
    sprintf(m_type, "%s", behaviorType);

    mUrl.appendChange("startTime", m_start);
    mUrl.appendChange("endTime", m_end);
    mUrl.appendChange("behaviorType", m_type);

    return obtain();
}

void BehaviourClassifySummary::profile()
{
    GEN_Printf(LOG_INFO, "---------驾驶行为-分类统计查询--------");
    GEN_Printf(LOG_DUMP, "总能耗: %s", mItem.fuels.c_str());
    GEN_Printf(LOG_DUMP, "总时长: %s", mItem.time.c_str());
    GEN_Printf(LOG_DUMP, "总次数: %s", mItem.num.c_str());
    GEN_Printf(LOG_DUMP, "总里程: %s", mItem.miles.c_str());
    GEN_Printf(LOG_DUMP, "总油耗: %s", mItem.oilFuel.c_str());
    GEN_Printf(LOG_DUMP, "总电耗: %s", mItem.electricFuel.c_str());
    GEN_Printf(LOG_DUMP, "总气耗: %s", mItem.gasFuel.c_str());
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int BehaviourClassifySummary::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        dataJson = records;
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, fuels, mItem.fuels);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, time, mItem.time);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, num, mItem.num);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, miles, mItem.miles);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, oilFuel, mItem.oilFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, electricFuel, mItem.electricFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, gasFuel, mItem.gasFuel);
        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
